﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
using System.Web.Mvc;

namespace LynxWebLog
{
    [DataContract(Namespace = "http://schemas.datacontract.org/2004/07/LynxBlogAPI.Models")]
    public class PostData
    {
        [DataMember]
        public int postid { get; set; }
        [DataMember, Required]
        public string blogid { get; set; }
        [DataMember, Required]
        public string title { get; set; }
        [DataMember, Required, AllowHtmlAttribute]
        public string description { get; set; }
        [DataMember]
        public string mt_text_more { get; set; }
        [DataMember]
        public string mt_keywords { get; set; }
        [DataMember]
        public string wp_slug { get; set; }
        [DataMember]
        public string mt_basename { get; set; }
        [DataMember]
        public string mt_excerpt { get; set; }
        [DataMember]
        public string wp_author_id { get; set; }
        public string[] categories { get; set; }
        [DataMember]
        public bool IsPublished { get; set; }
        [DataMember, DataType(DataType.Date), DisplayFormat(DataFormatString="{0:yyyy-MM-dd}", ApplyFormatInEditMode=true)]
        public DateTime dateCreated { get; set; }
        [DataMember]
        public string license { get; set; }
        [DataMember]
        public bool CommentsClosed { get; set; }

        public PostData() { }

        public PostData(Post p, string blogid, List<CategoryData> cl, string license = "", bool commentsclosed = true)
        {
            this.postid = p.postid;
            this.blogid = blogid;
            this.title = p.title;
            this.description = p.description;
            this.mt_text_more = p.mt_text_more;
            this.mt_keywords = p.mt_keywords;
            this.wp_slug = p.wp_slug;
            this.mt_basename = p.mt_basename;
            this.mt_excerpt = p.mt_excerpt;
            this.wp_author_id = p.wp_author_id;
            
            List<string> tempC = new List<string>();
            foreach (CategoryData c in cl) {
                tempC.Add(c.title);
            }
            categories = tempC.ToArray();
            
            this.IsPublished = p.IsPublished;
            if (p.dateCreated == DateTime.MinValue) p.dateCreated = DateTime.Now;
            this.dateCreated = p.dateCreated;
            this.license = license;
            this.CommentsClosed = commentsclosed;
        }

    }
}